﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mimoza.Apps.Console.View
{
    public partial class QueryByPlatformView : HostsViewBase
    {
        public QueryByPlatformView()
        {
            InitializeComponent();
        }

        public override void OnActivate()
        {
            base.OnActivate();
            Fill();
        }

        public override void OnItemUpdate(Mimoza.ObjectLayer.Host host, ListViewItem item)
        {
            base.OnItemUpdate(host, item);

            ListViewGroup viewGroup = GetViewGroup(host);

            item.Group = viewGroup;
        }

        private ListViewGroup GetViewGroup(Mimoza.ObjectLayer.Host host)
        {
            string hostPlatformFullName = host.Model + "-" + host.Version + "-" + host.OS;

            ListViewGroup viewGroup = FindViewGroup(hostPlatformFullName);
            if (viewGroup == null)
            {
                viewGroup = new ListViewGroup();
                viewGroup.Header = hostPlatformFullName;
                viewGroup.Tag = hostPlatformFullName;
                listView.Groups.Add(viewGroup);
            }

            return viewGroup;
        }

        private ListViewGroup FindViewGroup(string hostPlatformFullName)
        {
            return listView.Groups.Cast<ListViewGroup>().
                FirstOrDefault(h => (h.Tag as string) == hostPlatformFullName);
        }
    }
}
